//
//  GPFriendRequestController.m
//  Keping
//
//  Created by 23 on 2017/12/9.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPFriendRequestController.h"
#import "GPContactRequest.h"

@interface GPFriendRequestController ()

@property (weak, nonatomic) IBOutlet UITextField *messageTextField;

@property (weak, nonatomic) IBOutlet UITextField *aliasTextField;

@end

@implementation GPFriendRequestController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"Friend Request";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Send" style:UIBarButtonItemStyleDone target:self action:@selector(sendRequest)];
    self.aliasTextField.text = self.nickname;
    
}


- (void)sendRequest
{
    //检查
    NSString *message = self.messageTextField.text;
    NSString *alias   = self.aliasTextField.text;
    
    if (message.length == 0) {
        [self showErrorText:@"Please enter your message"];
        return;
    }
    
    if (alias.length == 0) {
        [self showErrorText:@"Please enter your alias"];
        return;
    }
    
    //发起请求
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:kIMAddFriendURL];
    request.isPost = YES;
    request.params = @{@"lableName":alias,
                       @"remarks":message,
                       @"requestPartyId":self.friendId};
    
    [self showLoading];
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [self hideHUD];
        
        NSDictionary *response = request.responseJSONObject;
        if ([response[@"statusCode"] integerValue] != 200) {
            
            [self showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
            return;
        }
        
        [self showSuccess:@"success"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        
        
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        
    }];
}


@end
